package com.zkingsoft.actions.customer;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tencent.business.JsApiPayBusiness;
import com.tencent.business.ScanPayBusiness;
import com.tencent.common.Configure;
import com.tencent.protocol.param_protocol.BrandWCPayRequestData;
import com.tencent.protocol.pay_protocol.JsApiPayReqData;
import com.tencent.protocol.pay_protocol.JsApiPayResData;
import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.model.Fy;
import com.zkingsoft.model.Users;
import com.zkingsoft.model.ZdOrder;
import com.zkingsoft.plugin.util.LogUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.FyService;
import com.zkingsoft.services.UsersService;
import com.zkingsoft.services.ZdOrderService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-10-07 08:53
 */
@Controller
@RequestMapping(value = "customer/goodsPay")
public class GoosPay extends BaseController {
	@Resource
	private ZdOrderService zdOrderService;

	@Resource
	private FyService fyService;
	@Resource
	private UsersService usersService;

	/**
	 * 
	 * @Description: 创建微信预付订单并使用微信内置浏览器发起支付
	 * @author:姜友瑶
	 * @return
	 * @throws Exception
	 *             返回类型 String
	 * @date 2016年10月14日
	 */
	@RequestMapping(value = "/createTradeByWXBrowse")
	public String createPrapareOrder(String page) throws Exception {

		// 获取订单信息
		ZdOrder order = zdOrderService.findByNo(page);
		LogUtil.info(this.getClass(), "createPrapareOrder orderNo=" + order.getOrderNo());
		// 创建微信支付预付接口
		JsApiPayBusiness jsApiPayBusiness = new JsApiPayBusiness();
		// 设置预付请求参数，并获取预付请求结果
		// 这里的用户opein最好每次去拿新的，以免出现在别人微信中登录自己的账号。出现支付 账号与请求账号不一致的情况
		double price = order.getTotal() * 100;

		String spInfo = "";
		if (order.getSource().equals("金币充值")) {

			spInfo = "金币充值-";
		} else {
			spInfo = "房源信息置顶费用-";
		}

		JsApiPayReqData jsApiPayReqData = new JsApiPayReqData(spInfo + order.getSpInfo(), order.getOrderNo(),
				(int) price, WebUtil.getRequest().getRemoteAddr(), getMe().getWeixinId());
		// 创建预付订单并返回请求结果
		JsApiPayResData result = jsApiPayBusiness.createPrapareOrder(jsApiPayReqData);
		// 把预付订单的信息存放在request域中
		WebUtil.getRequest().setAttribute("msg", result.toString());
		WebUtil.getRequest().setAttribute("order", order);
		if (result.getReturn_code().equals(Configure.SUCCESS) && result.getReturn_code().equals(Configure.SUCCESS)) {
			// 请求成功
			// 构建BrandWCPayRequest发起支付需要的参数
			BrandWCPayRequestData payData = new BrandWCPayRequestData(result.getPrepay_id());
			WebUtil.getRequest().setAttribute("payData", payData);
			return "/customer/fyPay";
		} else {
			// 请求失败
			LogUtil.info(this.getClass(), "request prepay_id fall  return_msg=" + result.getReturn_msg());
			return "/common/404";
		}
	}

	/**
	 * 新增或者修改页面
	 * @throws Exception 
	 * @throws NumberFormatException 
	 */
	@RequestMapping(value = "/fyOrderPaySuccess")
	public @ResponseBody AjaxResult fyOrderPaySuccess(String page) throws NumberFormatException, Exception {
		ZdOrder order = zdOrderService.findByNo(page);
		if (order == null) {
			// 系统中没有找到订单
			return new AjaxResult(AjaxResult.STATUS_ERR, "支付失败", "支付失败");
		} else if (!"待支付".equals(order.getRemark())) {
			// 只有待付款订单可以进行付款
			return new AjaxResult(AjaxResult.STATUS_ERR, "支付失败", "支付失败");
		}
		// 判断订单是否存在
		ScanPayBusiness bus = new ScanPayBusiness();
		// 查询订单是否支付成功
		if (bus.doOnePayQuery(order.getOrderNo())) {
			order.setRemark("支付成功");
			zdOrderService.modify(order);
			String topage = "";
			if (order.getSource().equals("金币充值")) {
				topage = "customer/redirect/jb";
				Users user = getMe();
				user.setAge((int) Double.parseDouble(order.getTotal() + "") + user.getAge());
				usersService.modify(user);
				WebUtil.getSession().setAttribute(SystemConstance.LOGIN_KEY, user);
			} else {
				topage = "customer/redirect/myfy";
				Fy fy = fyService.findById(order.getFyId());
				fy.setGlIsTop("是");
				fyService.modify(fy);
			}
			return new AjaxResult(AjaxResult.STATUS_OK, topage, "支付成功");
		} else {
			return new AjaxResult(AjaxResult.STATUS_ERR, "", "支付失败");
		}
	}

	public static void main(String[] args) {
		System.out.println((int) Double.parseDouble("10.0"));
	}
}
